/**
 * @desc 函数防抖
 * @param {*} func
 * @param {*} wait
 * @param {*} immediate
 * @returns
 */
export default function debounce(func, wait, immediate = true) {
  let timeout
  return function() {
    const context = this
    const args = arguments
    if (timeout) clearTimeout(timeout)
    if (immediate) {
      var callNow = !timeout
      timeout = setTimeout(() => {
        timeout = null
      }, wait)
      if (callNow) func.apply(context, args)
    } else {
      timeout = setTimeout(function() {
        func.apply(context, args)
      }, wait)
    }
  }
}

